/* 
 * Advanced Foundation Classes
 * Copyright (C) 2000/2004  Fabio Rotondo - Andrea Galimberti
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *  
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *  
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "../../afc.h"
#include "../pyconnector.h"


int window_get_template ( DynamicClass * dyn )
{
	char * foo = afc_array_master_first ( dyn->args );

	dyn->result = "TITLE W H";
	dyn->result_type = AFC_DYNAMIC_CLASS_RESULT_TYPE_STRING;

	printf ( "You've just executed the get_template method in window class (%x)!\n", ( int ) dyn );
	printf ( "foo: %s\n", foo );

	return ( AFC_ERR_NO_ERROR );
}

DynamicClass * window_new_instance ()
{
	DynamicClass * dyn = afc_dynamic_class_new ();

	if ( dyn == NULL ) return NULL;

	afc_dynamic_class_add_method ( dyn, "get_template", "S", window_get_template );

	afc_dynamic_class_set_var ( dyn, AFC_DYNAMIC_CLASS_VAR_KIND_NUM, "today", ( void * ) 482002 );

	return ( dyn );
}

int window_del_instance ( DynamicClass * dyn )
{
	if ( dyn != NULL ) afc_dynamic_class_delete ( dyn );

	return ( AFC_ERR_NO_ERROR );
}


/*** MAIN ***/

int main ( int argc, char ** argv )
{
	AFC * afc = afc_new ();
	PyConnector * pycon = afc_pyconnector_new ();
	DynamicClassMaster * dm = afc_dynamic_class_master_new ();
	FILE * fp = NULL;

	if ( pycon == NULL ) {
		fprintf ( stderr, "Cannot allocate PyConnector instance\n" );
		return 1;
	}

	afc_dynamic_class_master_add ( dm, "window", NULL, window_new_instance, window_del_instance, NULL );

	if ( afc_pyconnector_init ( pycon, dm, argv [ 0 ] ) == AFC_ERR_NO_ERROR ) {

		fp = fopen ( "./testscript.py", "r" );

		if ( PyRun_SimpleFile ( fp, "./testscript.py" ) != 0 ) {
			fprintf ( stderr, "Error in executing the script\n" );
		}

		fclose ( fp );

		Py_Finalize ();
	}
	else fprintf ( stderr, "Error in PyConnector initialization\n" );


	/* FIRST delete the PyConnector instance, THEN delete the DynamicClassMaster instance,
	   not the other way around. */

	afc_pyconnector_delete ( pycon );

	afc_dynamic_class_master_delete ( dm );

	afc_delete ( afc );


	return ( 0 );
}

